package com.qc.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qc.sys.common.DataGridView;
import com.qc.sys.common.ResultObj;
import com.qc.sys.dao.SysLoginfoDao;
import com.qc.sys.entity.SysLoginfo;
import com.qc.sys.service.SysLoginfoService;
import com.qc.sys.vo.LogInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * (SysLoginfo)表服务实现类
 *
 * @author AltmanQccc
 * @since 2020-08-27 14:55:41
 */
@Service
public class SysLoginfoServiceImpl extends ServiceImpl<SysLoginfoDao, SysLoginfo> implements SysLoginfoService {

    @Autowired
    private SysLoginfoDao loginfoDao;


    @Override
    public DataGridView queryAllLogInfo(LogInfoVo logInfoVo) {
        IPage<SysLoginfo> page=new Page<>(logInfoVo.getPage(),logInfoVo.getLimit());
        QueryWrapper<SysLoginfo> qw=new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(logInfoVo.getLoginname()),"loginname",logInfoVo.getLoginname());
        qw.like(StringUtils.isNotBlank(logInfoVo.getLoginip()),"loginip",logInfoVo.getLoginip());
        qw.ge(logInfoVo.getStartTime()!=null,"logintime",logInfoVo.getStartTime());
        qw.le(logInfoVo.getEndTime()!=null,"logintime",logInfoVo.getEndTime());
        qw.orderByDesc("logintime");
        this.loginfoDao.selectPage(page,qw);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @Override
    public ResultObj removeLoginfoByid(Integer id) {
        try {
            this.baseMapper.deleteById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    @Override
    public ResultObj batchDeleteLoginfoByid(Integer[] ids) {
        //1.首先对传过来的ids进行判断，是否为空，是否长度大于0，false执行 {code:-1,msg:"传入ids异常"}
        try {
            if (null != ids&&ids.length>0){
                List<Integer> idList = new ArrayList<>();
                for (Integer id : ids) {
                    idList.add(id);
                }
                this.loginfoDao.deleteBatchIds(idList);
                return ResultObj.DELETE_SUCCESS;
            }else {
                return new ResultObj(-1,"传入ids异常");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
}